package com.CmJava.controller;


import com.CmJava.entity.project.ProjectDisplay;
import com.CmJava.entity.project.ProjectModel;
import com.CmJava.entity.ResultEntity;
import com.CmJava.service.ProjectService;
import com.CmJava.utils.DownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@Controller
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    // 获取全部的技术选型信息（id、技术名）
    @RequestMapping("techFilter")
    @ResponseBody
    public ResultEntity techFilter(){
        ResultEntity resultEntity= projectService.getTechFilter();
        return resultEntity;
    }

    // 获取所有mysql项目概要（只包括名字、描述、技术id）
    @RequestMapping("projectDisplay")
    @ResponseBody
    public ResultEntity projectDisplay(HttpServletRequest request){
        ResultEntity resultEntity = projectService.getProjectDisplay();
        return resultEntity;
    }

    // 获取单个项目的图片
    @RequestMapping("getProjectImg")
    @ResponseBody
    public ResultEntity getProjectImg(@RequestParam("projectId") String projectId){
        ResultEntity resultEntity = projectService.getProjectImg(projectId);
        return resultEntity;
    }

    // 获取所有mysql项目概要（只包括项目名）
    @RequestMapping("projectList")
    @ResponseBody
    public ResultEntity projectList(){
        ResultEntity resultEntity = projectService.getProjectList();

        return resultEntity;
    }

    // 获取单个mysql项目概要（全部信息）
    @RequestMapping("getProjectMysql")
    @ResponseBody
    public ResultEntity getProjectMysql(@RequestParam("projectId") String projectId){
        ResultEntity resultEntity = projectService.getProjectMysql(projectId);
        return resultEntity;
    }

    // 获取单个项目Redis具体描述
    @RequestMapping("getProjectRedis")
    @ResponseBody
    public ResultEntity getRedisProject(@RequestParam("projectId") String projectId) throws Exception{
        String key="project"+projectId+"_for_MyWeb";
        ResultEntity resultEntity= projectService.getRedisProject(key);
        return resultEntity;
    }


    // 获取添加该项目对应的新项目号
    @RequestMapping("getNewProjectId")
    @ResponseBody
    public ResultEntity getNewProjectId() {
        ResultEntity resultEntity= projectService.getNewProjectId();
        return resultEntity;
    }

    // 初始化项目的评论表
    @RequestMapping("adminHtml/addProjectCommentMysql")
    @ResponseBody
    public ResultEntity addProjectCommentMysql(@RequestParam("projectId") String projectId) {
        ResultEntity resultEntity= projectService.addProjectCommentMysql(projectId);
        return resultEntity;
    }

    // 添加项目到mysql
    @RequestMapping("adminHtml/addProjectMysql")
    @ResponseBody
    public ResultEntity addProjectMysql(@RequestParam("projectId") String projectId,
                                        @RequestParam("projectName") String projectName,
                                        @RequestParam("projectDescription") String projectDescription,
                                        @RequestParam("techId") String techId,
                                        @RequestParam("imgName") String imgName,
                                        @RequestParam("projectUrl") String projectUrl) {

        ProjectDisplay projectDisplay = new ProjectDisplay(Integer.parseInt(projectId)
                , projectName, projectDescription, techId, imgName
                , projectUrl, ""
                , null);
        ResultEntity resultEntity= projectService.addProjectMysql(projectDisplay);
        return resultEntity;
    }


    // 添加该项目到redis
    @RequestMapping("adminHtml/addProjectRedis")
    @ResponseBody
    public ResultEntity addProjectRedis(@RequestParam("projectId") String projectId,
                                   @RequestParam("projectRedisData") String projectRedisData) {
        String key="project"+projectId+"_for_MyWeb";
        ResultEntity resultEntity= projectService.addProjectRedis(key,projectRedisData);
        return resultEntity;
    }

    //备份某篇项目
    @RequestMapping("adminHtml/backupProject")
    @ResponseBody
    public ResultEntity backupProject(@RequestParam("projectId") String projectId) {
        return projectService.backupProject(projectId);
    }

    //下载博客项目
    @RequestMapping("adminHtml/downBackupProject")
    @ResponseBody
    public ResultEntity downBackupProject(HttpServletResponse response) {
        File zipFile = (File) projectService.downBackupProject().getData();
        DownUtil.downFileOutsideJar(response,zipFile);
        return null;
    }

    //todo 以下全部暂定
    // 添加子模块图片到jar中
    @Deprecated
    @RequestMapping("addImgs")
    @ResponseBody
    public ResultEntity addImgs(@RequestParam("fileImage")MultipartFile fileImage,
                                @RequestParam("title")String title) {
        if(fileImage.isEmpty()){
            return ResultEntity.failureWithoutMessage();
        }
        try {
            ResultEntity resultEntity= projectService.addImgs(title,fileImage);
            return resultEntity;
        } catch (Exception e) {
            return ResultEntity.failureWithoutMessage();
        }
    }

    // 获取模块数
    @Deprecated
    @RequestMapping("getModelAndChildNum")
    @ResponseBody
    public ResultEntity getModelAndChildNum(@RequestParam("projectId") String projectId) {
        String key="project"+projectId+"_for_MyWeb";
        return projectService.getModelAndChildNum(key);
    }

    // 获取项目信息
    @RequestMapping("getThisProject")
    @ResponseBody
    public ResultEntity getThisProject(@RequestParam("projectId") String projectId) {
        String key="project"+projectId+"_for_MyWeb";
        return projectService.getThisProject(key);
    }

    // 修改项目模块
    @Deprecated
    @RequestMapping("adminHtml/updateThisModle")
    @ResponseBody
    public ResultEntity updateThisModle(@RequestParam("projectId") String projectId,
                                        @RequestParam("modelId") String modelId,
                                        @RequestBody ProjectModel projectModel) {
        String key="project"+projectId+"_for_MyWeb";
        return projectService.updateThisModle(key,modelId,projectModel);
    }

    // 获取项目技术选型
    @RequestMapping("getThisProjectTech")
    @ResponseBody
    public ResultEntity getThisProjectTech(@RequestParam("projectId") String projectId) {
        return projectService.getThisProjectTech(projectId);
    }

    // 更新mysql项目信息
    @RequestMapping("adminHtml/updateProjectMysql")
    @ResponseBody
    public ResultEntity updateProjectMysql(@RequestBody ProjectDisplay projectDisplay) {
        return projectService.updateProjectMysql(projectDisplay);
    }

    // 更新项目前后端信息
    @RequestMapping("adminHtml/updateTech")
    @ResponseBody
    public ResultEntity updateTech(@RequestParam("projectId") String projectId,
                                           @RequestParam("front") String front,
                                           @RequestParam("back") String back) {
        String key="project"+projectId+"_for_MyWeb";
        return projectService.updateTech(key,front,back);
    }



    // 删除项目所有的图片
    @RequestMapping("adminHtml/deleteAllImg")
    @ResponseBody
    public ResultEntity deleteAllImg() {
        projectService.deleteAllImg("5");
        return null;
    }

    //删除模块
    @RequestMapping("adminHtml/deleteProjectModel")
    @ResponseBody
    public ResultEntity deleteProjectModel(@RequestParam("projectId") String projectId,@RequestParam("modelIndex") String modelIndex) {
        return projectService.deleteProjectModel(projectId,Integer.parseInt(modelIndex)-1);
    }

    //删除子模块
    @RequestMapping("adminHtml/deleteProjectChildModel")
    @ResponseBody
    public ResultEntity deleteProjectChildModel(@RequestParam("projectId") String projectId,@RequestParam("childModelIndex") String childModelIndex) {
        int modelIndex = Integer.parseInt(childModelIndex.split("-")[0]) - 1;
        int childIndex = Integer.parseInt(childModelIndex.split("-")[1]) - 1;

        return projectService.deleteProjectChildModel(projectId,modelIndex,childIndex);
    }
}

